#include "data_element.h"

namespace Database
{

unsigned DataElement::lastAddedId(sqlite::database& database) const
{
    unsigned result;
    database << "SELECT last_insert_rowid();"
        >> [&result](unsigned id)
    {
        result = id;
    };
    return result;
}

bool DataElement::isNull() const
{
    return bool(m_element);
}

void DataElement::setElement(Elements::Element* element)
{
    m_element.reset(element);
}

std::shared_ptr<Elements::Element> DataElement::element() const
{
    return m_element;
}

Elements::ElementType DataElement::elementType() const
{
    if (m_element)
        return m_element->elementType();

    return Elements::ElementType::None;
}

unsigned DataElement::id() const
{
    return m_element->id();
}

std::string DataElement::questionMarks(unsigned size)
{
    std::string result;
    for (unsigned i = 0; i < size; ++i)
    {
        result.append("?");
        if (i != size - 1)
            result.append(",");
    }
    return result;
}

} // namespace Database
